//设置根路径
const list = document.querySelector('.top');
axios.defaults.baseURL = 'http://ajax-api.itheima.net'
axios({
  method: 'GET',
  url: '/api/category/top'
}).then(({ data: res }) => {
  console.log(res);
  const axiosArr = res.data.map(item => {
    return axios({ method: 'GET', url: '/api/category/sub?id=' + item.id })
  })
  return Promise.all(axiosArr)
}).then((res) => {
  //得到二级分类的数据
  const html = res.map(({ data: item }) => {
    // console.log(item);
    return `
          <li>
              <a href=" ">${item.data.name}</a>
              <img src="${item.data.picture}"alt=""/>
              <ul class="sub">
              ${item.data.children.map((item) => {
                return ` 
                   <li>
                    <a href="javascript:;">
                    <span>${item.name}</span>
                    <img src="${item.picture}"alt=""/>
                    </a>
                 </li>
                     `
    }).join('')}
 </ul>
 </li>`
  }).join('')
  list.innerHTML = html
})


